<?php

abstract class ctrl {

    protected $ret = 0; //返回值（用于判断是否添加|删除|修改成功）添加成功的获取ID号
    protected $tables = array(); //储存MYSQL返回的表数据(是一个带名字的数组)二维表
    protected $assoc = array(); //储存一个带名字的数组（一维数组）
    public $search = array(); //由外部POST传入的搜索数组
    public $pageinfo = array(); //由外部POST传入的分页信息
    public $order = array(); //排序

    public function __set($property, $value) {
        die('不能ASSIGN这个属性[' . $property . '=' . json_encode($value) . ']！因为在｛控制器｝CLASS类里面未定义！');
    }

    //当CTRL的行为方法执行完之后通过这个调用数据
    public function getdata() {
        $data = array();
        $data['ret'] = $this->ret; //返回执行结果
        $data['tables'] = $this->tables; //二维表
        $data['assoc'] = $this->assoc; //一维数组
        $data['search'] = $this->search; //搜索条件
        $data['pageinfo'] = $this->pageinfo; //分页信息
        $data['order'] = $this->order; //排序
        return($data);
    }

    /*
     * 分页功能
     */

    public function page($table, $pageinfo) {
        if (is_numeric($pageinfo))
            return;
        //获取记录总数
        $pageinfo['totalCount'] = $table->field("count(*) AS totalCount")->getvalue();
        //获取要查询的页码
        $pageinfo['pageNum'] = (isset($pageinfo['pageNum']) && intval($pageinfo['pageNum']) >= 1) ? $pageinfo['pageNum'] : 1;
        //获取单页最大数量
        $pageinfo['numPerPage'] = (isset($pageinfo['numPerPage']) && intval($pageinfo['numPerPage']) >= 1) ? $pageinfo['numPerPage'] : 15;
        //判断是否超过最大页数
        $maxPageNum = ceil($pageinfo['totalCount'] / $pageinfo['numPerPage']);
        if ($pageinfo['pageNum'] > $maxPageNum)
            $pageinfo['pageNum'] = $maxPageNum;
        return($pageinfo);
    }

}